<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/5/28
 * Time: 15:22
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Help extends Base {
    /**
     * 列表展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            if(isset($keyword)&&$keyword!="")
            {
                $map['help_title'] = ['like',"%" . $keyword . "%"];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('help')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('help')
                ->where($map)
                ->page($NowPage,$limits)
                ->order('help_sort asc,help_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    /**
     * 修改排序
     * @return \think\response\Json
     */
    public function sort(){
        $param = input('');
        $res = Db::table('up_help')->where('help_id', $param['id'])->setField('help_sort', $param['value']);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' => '修改失败']);
        }
    }

    /***
     * @return \think\response\Json
     * 上架
     */
    public function up(){
        $task_id = input('param.id');
        $res = Db::name('help')->where('help_id',$task_id)->setField('help_status',1);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }

    /***
     * @return \think\response\Json
     * 操作
     */
    public function down(){
        $task_id = input('param.id');
        $res = Db::name('help')->where('help_id',$task_id)->setField('help_status',2);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }

    /***
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $task_id = input('param.id');
        $res = Db::name('help')->where('help_id',$task_id)->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }

    /***
     * @return mixed|\think\response\Json
     * 添加
     */
    public function add(){
        if(Request::instance()->isPost()){
            $help_title =input('help_title');
            $help_sort= input('help_sort');
            $help_content = input('help_content');
            $data['help_title'] =$help_title;
            $data['help_sort'] =$help_sort;
            $data['help_content'] =$help_content;
            $data['help_time'] =date('Y-m-d H:i:s',time());
            $res = Db::table('up_help')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }

    /***
     * @return mixed|\think\response\Json
     * 编辑
     */
    public function update(){
        if(Request::instance()->isPost()){
            $id = input('id');
            $help_title =input('help_title');
            $help_sort= input('help_sort');
            $help_content = input('help_content');
            $data['help_title'] =$help_title;
            $data['help_sort'] =$help_sort;
            $data['help_content'] =$help_content;
            $res = Db::table('up_help')->where('help_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $id = input('help_id');
        $info= Db::table('up_help')->where('help_id',$id)->find();
        $this->assign('id',$id);
        $this->assign('res',$info);
        return $this->fetch();
    }

    public function detail(){
        $id = input('help_id');
        $info= Db::table('up_help')->where('help_id',$id)->find();
        $this->assign('res',$info);
        return $this->fetch();
    }
}